﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using web.data.entities;

namespace web.bussines
{
    public class cart_service
    {     
        /// <summary>
        /// Get Cart By ProductID and shoppingCartID (GUID)
        /// </summary>
        /// <param name="shoppingCartId"></param>
        /// <param name="product"></param>
        /// <returns></returns>
        public cart GetCartByCartIdAndProductID(string shoppingCartId, product product)
        {
            using (var data = new baseEntities())
            {
                var cart = (from p in data.tbl_cart
                            where p.cart_id == shoppingCartId && p.product_id == product.product_id
                            select p).FirstOrDefault();
                return cart;
            }
        }

        /// <summary>
        /// Get Cart by recordID and shoppingCartID
        /// </summary>
        /// <param name="shoppingCartId"></param>
        /// <param name="recordID"></param>
        /// <returns></returns>
        public cart GetCartByCartIdAndRecordID(string shoppingCartId, int recordID)
        {
            using (var data = new baseEntities())
            {
                var cart = (from p in data.tbl_cart
                            where p.cart_id == shoppingCartId && p.record_id == recordID
                            select p).FirstOrDefault();
                return cart;
            }
        }

        /// <summary>
        /// Get List CartItem by shoppingCartID
        /// </summary>
        /// <param name="shoppingCartId"></param>
        /// <returns></returns>
        public List<cart> GetListCartItemByShoppingCartID(string shoppingCartId)
        {
            using (var data = new baseEntities())
            {
                var cart = (from p in data.tbl_cart
                            where p.cart_id == shoppingCartId
                            select p);
                return cart.ToList();
            }
        }

        /// <summary>
        /// Get total Number Product by shoppingCartId
        /// </summary>
        /// <param name="shoppingCartID"></param>
        /// <returns></returns>
        public int GetCountCartItem(string shoppingCartID)
        {
            using (var data = new baseEntities())
            {
                int? count = (from p in data.tbl_cart
                              where p.cart_id == shoppingCartID
                              select (int?)p.count).Sum();

                return count ?? 0;
            }

        }

        /// <summary>
        /// Get total number one product in CartItem 
        /// </summary>
        /// <param name="shoppingCartID"></param>
        /// <param name="productID"></param>
        /// <returns></returns>
        public int GetCountCartItemByProductID(string shoppingCartID, int productID)
        {
            using (var data = new baseEntities())
            {
                int? count = (from p in data.tbl_cart
                              where p.cart_id == shoppingCartID && p.product_id == productID
                              select (int?)p.count).Sum();

                return count ?? 0;
            }
        }

        
        /// <summary>
        /// Get Total Value of CartItem by shoppingCartID
        /// </summary>
        /// <param name="shoppingCartID"></param>
        /// <param name="languageID"></param>
        /// <returns></returns>
        public double GetTotal(string shoppingCartID)
        {
            using (var data = new baseEntities())
            {
                product_service proSvc = new product_service();
                var cartItems = GetListCartItemByShoppingCartID(shoppingCartID);

                double? totalPrice = 0;
                foreach (var item in cartItems)
                {
                    double? prices = GetCountCartItemByProductID(shoppingCartID, (int)item.product_id) * proSvc.GetProductPriceByID(item.product_id ?? 0);
                    double? dis = 0;
                    //chưa có discount
                    totalPrice += (prices - dis);
                }
                return totalPrice ?? 0;
            }

        }
        /// <summary>
        /// Update Total Value of Order 
        /// </summary>
        /// <param name="orderID"></param>
        /// <param name="totalValue"></param>
        public void UpdateTotalOrder(int orderID, double totalValue)
        {
            using (var data = new baseEntities())
            {
                var order = (from p in data.tbl_order
                             where p.order_id == orderID
                             select p).FirstOrDefault();
                order.total = totalValue;
                data.SaveChanges();
            }
        }

        /// <summary>
        /// Get Cart by RecordID
        /// </summary>
        /// <param name="recordID"></param>
        /// <returns></returns>
        public cart GetCartByRecordID(int recordID)
        {
            using (var data = new baseEntities())
            {
                var cart = (from p in data.tbl_cart
                            where p.record_id == recordID
                            select p).FirstOrDefault();
                return cart;
            }
        }
        /// <summary>
        /// Get Cart by CartID
        /// </summary>
        /// <param name="CartID"></param>
        /// <returns></returns>
        public cart GetCartByCartID(string CartID)
        {
            using (var data = new baseEntities())
            {
                var cart = (from p in data.tbl_cart
                            where p.cart_id == CartID
                            select p).FirstOrDefault();
                return cart;
            }
        }

        public List<cart> GetListCartItemsByCartID(string CartID)
        {
            using (var data = new baseEntities())
            {
                var list = (from p in data.tbl_cart
                            where p.cart_id == CartID
                            select p).ToList();
                return list;
            }
        }

        /// <summary>
        /// Add cart to Tbl Cart
        /// </summary>
        /// <param name="cart"></param>
        /// <returns></returns>
        public int AddToCart(cart cartItem)
        {
            using (var data = new baseEntities())
            {
                data.AddTotbl_cart(cartItem);
                data.SaveChanges();

                return cartItem.record_id;
            }
        }


        /// <summary>
        /// Update count in CartItem
        /// </summary>
        /// <param name="cartItem"></param>
        public void UpdateQuantityCart(cart cartItem)
        {
            using (var data = new baseEntities())
            {
                var cart = (from p in data.tbl_cart
                            where p.record_id == cartItem.record_id
                            select p).FirstOrDefault();

                cart.count = cartItem.count;
                data.SaveChanges();
            }
        }

        /// <summary>
        /// Update CartID when User Login
        /// </summary>
        /// <param name="cartItem"></param>
        public void UpdateCartId(cart cartItem)
        {
            using (var data = new baseEntities())
            {
                var cart = (from p in data.tbl_cart
                            where p.record_id == cartItem.record_id
                            select p).FirstOrDefault();

                cart.cart_id = cartItem.cart_id;
                data.SaveChanges();
            }
        }
        
        /// <summary>
        /// Delete one Item on Cart
        /// </summary>
        /// <param name="cartItem"></param>
        public void DeleteCartItem(cart cartItem)
        {
            using (var data = new baseEntities())
            {
                var cart = (from p in data.tbl_cart
                            where p.record_id == cartItem.record_id
                            select p).FirstOrDefault();

                data.DeleteObject(cart);
                data.SaveChanges();
            }
        }

        /// <summary>
        /// Create one OrderDetail for Order
        /// </summary>
        /// <param name="orderDetail"></param>
        /// <returns></returns>
        public int CreateOrderDetail(order_detail orderDetail)
        {
            using (var data = new baseEntities())
            {
                data.AddTotbl_order_detail(orderDetail);
                data.SaveChanges();

                return orderDetail.detail_id;
            }
        }

        
        /// <summary>
        /// Add Order to DB
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public int AddOrder(order order)
        {
            using (var data = new baseEntities())
            {
                data.AddTotbl_order(order);
                data.SaveChanges();

                return order.order_id;
            }
        }

        /// <summary>
        /// Get Customer Order by OrderID and CustomerID
        /// </summary>
        /// <param name="orderID"></param>
        /// <param name="cusID"></param>
        /// <returns></returns>
        public order GetOrderByOrderIDAndCustomerID(int orderID, int cusID)
        {
            using (var data = new baseEntities())
            {
                var order = (from p in data.tbl_order
                             where p.order_id == orderID && p.customer_id == cusID
                             select p).FirstOrDefault();
                return order;
            }
        }

        /// <summary>
        /// Get Count Product in Order
        /// </summary>
        /// <param name="orderID"></param>
        /// <returns></returns>
        public int GetCountProductInOrder(int orderID)
        {
            using (var data = new baseEntities())
            {
                int count = 0;
                var list = (from p in data.tbl_order
                            join it in data.tbl_order_detail on p.order_id equals it.order_id
                            where p.order_id == orderID
                            select it);
                foreach (var item in list)
                {
                    count += item.quantity ?? 0;
                }

                return count;
            }
        }
        /// <summary>
        /// Get Order by ID
        /// </summary>
        /// <param name="orderID"></param>
        /// <returns></returns>
        public order GetOrderByID(int orderID)
        {
            using (var data = new baseEntities())
            {
                var order = (from p in data.tbl_order
                             where p.order_id == orderID
                             select p).FirstOrDefault();

                return order;
            }
        }

       
        /// <summary>
        /// Get List Product by orderID
        /// </summary>
        /// <param name="orderID"></param>
        /// <returns></returns>
        public List<product> GetListProductByOrderID(int orderID)
        {
            using (var data = new baseEntities())
            {
                var list = (from p in data.tbl_order_detail
                            join pr in data.tbl_product on p.product_id equals pr.product_id
                            where p.order_id == orderID
                            select pr).ToList();
                return list;
            }
        }
       
        /// <summary>
        /// Get Customer By ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public customer GetCustomerByID(int id)
        {
            using (var data = new baseEntities())
            {
                var cus = (from p in data.tbl_customer
                           where p.customer_id == id
                           select p);
                return cus.FirstOrDefault();
            }
        }
        /// <summary>
        /// Get Customer by userID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public customer GetCustomerByUserID(int id)
        {
            using (var data = new baseEntities())
            {
                var cus = (from p in data.tbl_customer
                           where p.user_id == id
                           select p);
                return cus.FirstOrDefault();
            }
        }
        
    }
}
